package ch.zhaw.cctd.domain.player;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Representiert einen Spieler. Der Spieler ist verantwortlich für seinen
 * SpawnPoint und seine ConstructionZone sowie sein Geld (Ressourcen.
 * 
 * @author Rolf
 * 
 */
public class Player implements Serializable {

	/**
	 * Serializable ID
	 */
	private static final long serialVersionUID = -2769975578711242758L;

	// Player Informations
	private int playerId;
	private String name;
	private List<SpawnPoint> spawnPointList;
	private List<ConstructionZone> constructionZoneList;
	private int money;

	/**
	 * DefaultConstructor
	 * 
	 * @param name
	 *            Player Name
	 * @param playerId
	 *            Integer Value of the Player ID.
	 * @param local
	 *            If he is the local Player or not
	 * 
	 */
	public Player() {
		this.spawnPointList = new ArrayList<SpawnPoint>();
		constructionZoneList = new ArrayList<ConstructionZone>();
	}

	/**
	 * Constructor with Name
	 * 
	 * @param name
	 *            Player Name
	 * 
	 */
	public Player(String name) {
		this();
		setName(name);
	}

	/**
	 * Constructor with Name and Player Id
	 * 
	 * @param String
	 *            Player Name
	 * @param int PlayerID
	 * 
	 */
	public Player(String name, int playerId) {
		this();
		setName(name);
		setPlayerId(playerId);
	}

	/**
	 * Player Name get
	 * 
	 * @return
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * Player Name Set
	 * 
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Player id which represents his location
	 * 
	 * @return
	 */
	public int getPlayerId() {
		return this.playerId;
	}

	/**
	 * Change the Player ID
	 */
	public void setPlayerId(int playerId) {
		this.playerId = playerId;
	}

	/**
	 * add a Players Creep Spawpoint
	 * 
	 * @param spawnPoint
	 */
	public void addSpawnPoint(SpawnPoint spawnPoint) {
		this.spawnPointList.add(spawnPoint);
	}

	/**
	 * Returns the Players Creep SpawnPointList
	 * 
	 * @return SpawnPointList
	 */
	public List<SpawnPoint> getSpawnPointList() {
		return spawnPointList;
	}

	/**
	 * add a Players Construction Zone
	 * 
	 * @param mapConstructionZone
	 */
	public void addMapConstructionZone(ConstructionZone mapConstructionZone) {
		this.constructionZoneList.add(mapConstructionZone);
	}

	/**
	 * returns the Players Construction zone List
	 * 
	 * @return Construction Zone List
	 */
	public List<ConstructionZone> getMapConstructionZoneList() {
		return constructionZoneList;
	}

	/**
	 * Checks if a certain amount of Money is available
	 * 
	 * @param cost
	 * @return boolean true = yes
	 */
	public boolean hasRessource(int cost) {
		return (cost <= money);
	}

	/**
	 * Removes Money from the Money Stack
	 * 
	 * @param cost
	 * @return new Money status
	 */
	public int payRessource(int cost) {
		if (hasRessource(cost)) {
			money -= cost;
			return getRessource();
		} else {
			return getRessource();
		}
	}

	/**
	 * Adds Money to the Money Stack
	 * 
	 * @param amount
	 */
	public void receiveRessource(int amount) {
		money += amount;
	}

	/**
	 * Returns the current amount of Money
	 * 
	 * @return money
	 */
	public int getRessource() {
		return money;
	}
	
	public String toString() {
		return getPlayerId() + ": " + getName() + "(" + getRessource() + ")";
	}

}
